package org.jin.samples.web;

import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.jin.samples.model.ExamRole;
import org.jin.samples.model.vo.ExamRoleVO;
import org.jin.samples.model.vo.ExamUserVO;
import org.jin.samples.service.IManageStudentService;
import org.jin.samples.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class LoginController {
		
		@Autowired
		private IManageStudentService manageStudentService;
		
		@RequestMapping(value = "/login", method =RequestMethod.GET)
		public String toLogin(Model model){
			ExamUserVO userVO = new ExamUserVO();
			model.addAttribute("userVO",userVO);
			return "login/login";
		}
		
		@RequestMapping(value = "/index", method =RequestMethod.GET)
		public String toIndex(){
			return "index/index";
		}
		
		//进行登录
		@RequestMapping(value="/login", method = RequestMethod.POST)
		public String doLogin(RedirectAttributes redirectAttributes, ExamUserVO vo, HttpSession session){ 
			if (vo != null) {
				if(vo.getUserId()==null||vo.getUserId()==""){
					redirectAttributes.addFlashAttribute("message", "用户名为空");
				}else{
					redirectAttributes.addFlashAttribute("message", "用户名不正确");
					
				}
			}
			if(vo != null){
				ExamUserVO resultVO = manageStudentService.findByIdUser(vo.getUserId());
				if(resultVO != null){
					redirectAttributes.addFlashAttribute("message",null);
					if(vo.getPassword() != null){
						try {
							String encodePassword = MD5Utils.GetMD5Code(vo.getPassword());
							//密码正确的情况
							Date currentTime = null;
							Calendar curremtTimecalendar = Calendar.getInstance(); //创建日历对象
							curremtTimecalendar.setTime(new Date()); //初始化时间 new Date() -->50
							curremtTimecalendar.add(Calendar.MINUTE, -5); //减5分    -->45
							currentTime = curremtTimecalendar.getTime(); //currentTime -->45
							if(resultVO.getLastLockTime().before(currentTime)){
								//不在锁定的范围内
								System.out.println("不在锁定的范围内");
								if(encodePassword.equals(resultVO.getPassword())){
									//登录成功时清0
									resultVO.setLoginFailCount(0);
									manageStudentService.UpdataBy(resultVO);
									session.setAttribute("examUser", resultVO);
									
									System.out.println(resultVO.getExamRole());		
									for(ExamRole role : resultVO.getExamRole()){
											System.out.println(role.getName());						
									}
									return "redirect:/index";
								}else{
									resultVO.setLoginFailCount(resultVO.getLoginFailCount() + 1);
								
									if(vo.getPassword()==null||vo.getPassword()==""){
										redirectAttributes.addFlashAttribute("password", "密码为空");
									}else{
									redirectAttributes.addFlashAttribute("password", "密码错误");
									}
									
									if(resultVO.getLoginFailCount() == 3){
										resultVO.setLoginFailCount(0);
										resultVO.setLastLockTime(new Date());
										manageStudentService.UpdataBy(resultVO);
									}
									manageStudentService.UpdataBy(resultVO);
								}
							}else{
								System.out.println("目前账号被锁定，无法登陆，清稍等5分钟.");
								redirectAttributes.addFlashAttribute("password", "目前账号被锁定。请稍候5分钟在试");
							}
					} catch (NoSuchAlgorithmException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
			return "redirect:/login";	
	}
}